"""
time: 2024/10/24
title: SqlLink
message:

"""
import sqlite3


class SqlLink:
    def __init__(self):
        self.db_path = "data/config.db"

    def get_config_link(self):
        try:
            con = sqlite3.connect(self.db_path)
            # con.row_factory = sqlite3.Row
            return con
        except BaseException as e:
            self.db_path = "data/config.db"
            print(f"数据库检测发生异常, 异常摘要: 【{e}】")
            return self.get_config_link()

    def sql_fet(self, sql, font: list = None):
        """
        不需要提交类sql
        :param sql:
        :param font: 字段
        :return:
        """
        with self.get_config_link() as con:
            cur = con.cursor()
            cur.execute(sql)
            value = cur.fetchall()
        # 转为JSON
        json_result = [dict(zip(font, row)) for row in value]
        return json_result
